<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<script type="text/javascript">
	$(document).ready(function() {
		 $('#functionid').tree({
			checkbox : true,
			url : 'roleController.do?setAppAuthority&roleId=${roleId}',
			onLoadSuccess : function(node) {
				expandAll();
			},
			onClick: function(node){
				var isRoot =  $('#functionid').tree('getChildren', node.target);
				if(isRoot==''){
					var roleId = $("#rid").val();
					$('#operationListpanel').panel("refresh", "roleController.do?operationListForFunction&functionId="+node.id+"&roleId="+roleId);
					$('#dataRuleListpanel').panel("refresh", "roleController.do?dataRuleListForFunction&functionId="+node.id+"&roleId="+roleId);
				}			}
		}); 
	 	 
	});
	function mysubmit() {
		var roleId = $("#rid").val();
		var s = GetNode();
		doSubmit("roleController.do?updateAppAuthority&rolefunctions=" + s + "&roleId=" + roleId);
	}
	function GetNode() {
		var node = $('#functionid').tree('getChecked');
		var cnodes = '';
		var pnodes = '';
		var pnode = null; //保存上一步所选父节点
		for ( var i = 0; i < node.length; i++) {
			if ($('#functionid').tree('isLeaf', node[i].target)) {
				cnodes += node[i].id + ',';
				pnode = $('#functionid').tree('getParent', node[i].target); //获取当前节点的父节点
				while (pnode!=null) {//添加全部父节点
					pnodes += pnode.id + ',';
					pnode = $('#functionid').tree('getParent', pnode.target); 
				}
			}
		}
		cnodes = cnodes.substring(0, cnodes.length - 1);
		pnodes = pnodes.substring(0, pnodes.length - 1);
		return cnodes + "," + pnodes;
	};
	
	function expandAll() {
		var node = $('#functionid').tree('getSelected');
		if (node) {
			$('#functionid').tree('expandAll', node.target);
		} else {
			$('#functionid').tree('expandAll');
		}
	}
	
	var isSelectAll = true;
	function selecrAll() {
		var node = $('#functionid').tree('getRoots');
		for ( var i = 0; i < node.length; i++) {
			var childrenNode =  $('#functionid').tree('getChildren',node[i].target);
			for ( var j = 0; j < childrenNode.length; j++) {
				$('#functionid').tree(getTreeCheckOpt(isSelectAll),childrenNode[j].target);
			}
			
			if(childrenNode.length == 0){
				$('#functionid').tree(getTreeCheckOpt(isSelectAll),node[i].target);
			}
	    }
		
		isSelectAll = !isSelectAll;
	}
	
	function getTreeCheckOpt(isCheck){
		if(isCheck){
			return 'check';
		}else{
			return 'uncheck';			
		}
	}
	
	function reset() {
		
		$.ajax({
			url:"roleController.do?resetRole",
			type:"post",
			data:{
				roleId:$("#rid").val()
			},
			success:function(){
				$('#functionid').tree('reload');		
			}
		});
	}

	$('#selecrAllBtn').linkbutton({   
	}); 
	$('#resetBtn').linkbutton({   
	});   
	$('#mysubmit').linkbutton({   
	}); 
	
	
</script>
  <style>
  	.toolbar{
  		height:30px;
    	position: fixed;
    	width: 100%;
   	    background: white;
   	    padding:0;
   	    margin-top: -11px;
   	    padding-top: 5px;
  	}
  	
  	#functionid{
  		margin-top:29px;
  	}
  	
   
  </style>
 
	<input type="hidden" name="roleId" value="${roleId}" id="rid"> 
	
	<div class="toolbar">
		<a id="selecrAllBtn" onclick="selecrAll();"><t:mutiLang langKey="select.all"/></a> 
		<%-- <a id="resetBtn" onclick="reset();"><t:mutiLang langKey="common.reset"/></a> --%>
		<a id="mysubmit" onclick="mysubmit();">保存</a> 
	</div>
	
	<ul id="functionid"></ul> 

